package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    //注入UserDao
    @Autowired
    private UserDao userDao;

    //注入RoleDao
    @Autowired
    private RoleDao roleDao;

    //注入PermissionDao
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    //根据用户名查询用户信息
    public User findUserByUsername(String username) {
        //调用UserDao中的根据用户名查询用户的方法
        User user = userDao.findUserByUsername(username);

        if (user == null) {
            return null;
        }
        //获取userId
        Integer userId = user.getId();
        //调用RoleDao,查询该用户拥有的角色
        Set<Role> roles = roleDao.findRoleByUserId(userId);
        if (roles != null && roles.size() > 0) {
            //遍历角色
            for (Role role : roles) {
                //获取角色id
                Integer id = role.getId();
                //调用RoleDao,查询该角色拥有的权限
                Set<Permission> permissions = permissionDao.findPermissionByRoleId(id);

                if (permissions != null && permissions.size() > 0) {
                    //添加权限
                    role.setPermissions(permissions);
                }
            }
            //添加角色
            user.setRoles(roles);
        }
        return user;
    }

    /**
     * 用户动态数据添加
     */

    @Override
    public void add(User user, Integer[] roleIds) {

        userDao.add(user);
        Integer userId = user.getId();
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("user_id", userId);
                map.put("role_id", roleId);
                userDao.selectUserAndRole(map);
            }
        }

    }



    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        //调用DAO层
        Page<User> page = userDao.selectByCondition(queryString);
        List<User> result = page.getResult();
        for (User user : result) {
            user.setPassword(null);
        }
        return new PageResult(page.getTotal(),result);
    }


    //编辑用户
    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }


    @Override
    public List<Integer> findUserIdByRoleId(Integer id) {
        return userDao.findUserIdByRoleId(id);
    }

    @Override
    public void editPassword(User user) {
        userDao.editPassword(user);
    }


    @Override
    public void delete(Integer id) {

        userDao.deleteAssocation(id);
        userDao.deleteById(id);
    }


    @Override
    public void edit(User user, Integer[] roleIds) {
        Integer userId = user.getId();
        userDao.deleteAssocation(userId);
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("user_id", userId);
                map.put("role_id", roleId);
                userDao.selectUserAndRole(map);
            }
        }
        if(user.getPassword()!=null){
            user.setPassword(passwordEncoder.encode(user.getPassword()));
        }
        userDao.edit(user);

    }



}
